function SetCookie(name, value) //两个参数，一个是cookie的名子，一个是值
{
    var Days = 99999999999; //此 cookie 将被保存 30分钟
    var exp = new Date(); //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 60 * 1000);
    document.cookie = name + "=" + value + ";expires=" + exp.toGMTString() + ";path=/";
}

function getCookie(name) //取cookies函数       
{
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return (arr[2]);
    return null;
}

function delCookie(name) //删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
}

function clearCookie() {
    var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
    if (keys) {
        for (var i = keys.length; i--;)
            document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() + ";path=/";
    }
}


/**  ----------cookie----------结束**/
var ajaxPoint = null;

function callGetByJson(actionType, actionUrl, paramData, successFun, errorFun, newTimeout) {

    var mytimeout = 10000;
    var isyb = true;
    if (typeof successFun != "function" || typeof errorFun != "function" ||
        !(actionType == "POST" || actionType == "GET") ||
        actionUrl == undefined || paramData == undefined) {
        return;
    };

    var inTimeout = mytimeout;
    if (newTimeout != undefined && newTimeout != null) {
        inTimeout = newTimeout;
    };
    ajaxPoint = $.ajax({
        type: actionType,
        timeout: inTimeout,
        url: actionUrl,
        cache:false,
        data: paramData,
        dataType: 'json',
        success: function(msg) {
            try {
                var tmpData = msg;
                successFun(msg);
                System.out.println(msg);
            } catch (e) {} finally {

            }
        },
        error: function() {
            try {
                errorFun();
            } catch (e) {} finally {

            }
        }
    });

};

//公共ajax 调用

var basurl = 'http://home.yogovi.vip/home/';


var kfAjaxGet = { //系统ajax调用
    //会员登录
    loginIn: function(param, callBack) {
        var url = basurl + 'vcode/checkVcode';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //发送验证码
    sendCode: function(param, callBack) {
        var url = basurl + 'vcode/sendVcode';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //用户列表接口
    userList: function(param, callBack) {
        var url = basurl + 'user/list';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //全部节点
    nodeList: function(param, callBack) {
        var url = basurl + 'projectNode/list';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //项目列表
    projectList: function(param, callBack) {
        var url = basurl + 'project/list';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //新增修改节点
    editNode: function(param, callBack) {
        var url = basurl + 'projectNode/save';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //删除节点
    deleteNode: function(param, callBack) {
        var url = basurl + 'projectNode/delete';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //节点详情
    nodeDetail: function(param, callBack) {
        var url = basurl + 'projectNode/nodeDetail';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //发布动态
    projectNodeLog: function(param, callBack) {
        var url = basurl + 'projectNodeLog/create';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //我的任务
    orderList: function(param, callBack) {
        var url = basurl + 'projectNode/listPage';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //任务详情
    orderDetail: function(param, callBack) {
        var url = basurl + 'projectNode/detail';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //新建项目
    createProject: function(param, callBack) {
        var url = basurl + 'project/create';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //项目详情
    projectDetail: function(param, callBack) {
        var url = basurl + 'project/detail';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //删除动态
    deleteDt: function(param, callBack) {
        var url = basurl + 'projectNodeLog/delete';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //跳转下一节点
    nextJd: function(param, callBack) {
        var url = basurl + 'projectNode/next';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    //派工
    doJob: function(param, callBack) {
        var url = basurl + 'project/send';
        callGetByJson("POST", url, param, function(json) {
            callBack(json);
        }, function() {}, 100000);
    },
    
}


//获取url参数
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return '';
}

//获取数据字典
function com_getDictItems(dictValues) {
    var dictMap = {};
    $.ajax({
        type: "POST",
        url: basurl + "a/36",
        async: false,
        data: {
            'dictValues': dictValues
        },
        dataType: 'json',
        success: function(data) {
            if (data && data.code == 200 && data.data) {
                for (var i = 0; i < data.data.length; i++) {
                    dictMap[data.data[i].dictValue] = {};
                    dictMap[data.data[i].dictValue].items = data.data[i].items;
                    dictMap[data.data[i].dictValue].nameMap = {};
                    for (var k = 0; k < data.data[i].items.length; k++) {
                        var obj = data.data[i].items[k];
                        dictMap[data.data[i].dictValue].nameMap[obj.itemValue] = obj.itemName;
                    }
                }
            } else {
                $.alert(data.message);
            }
        }
    });
    return dictMap;
}
//获取数据字典互换版本
function com_getDictItemsKey(dictValues) {
    var dictMap = {};
    $.ajax({
        type: "POST",
        url: basurl + "a/36",
        async: false,
        data: {
            'dictValues': dictValues
        },
        dataType: 'json',
        success: function(data) {
            if (data && data.code == 200 && data.data) {
                for (var i = 0; i < data.data.length; i++) {
                    dictMap[data.data[i].dictValue] = {};
                    dictMap[data.data[i].dictValue].items = data.data[i].items;
                    dictMap[data.data[i].dictValue].nameMap = {};
                    for (var k = 0; k < data.data[i].items.length; k++) {
                        var obj = data.data[i].items[k];
                        dictMap[data.data[i].dictValue].nameMap[obj.itemName] = obj.itemValue;
                    }
                }
            } else {
                $.alert(data.message);
            }
        }
    });
    return dictMap;
}
//日期格式化
Date.prototype.format = function(fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}
//时间戳转换
function capitalize(value) {
    var date = new Date(parseInt(value));
    var tt = [date.getFullYear(), checkNum(date.getMonth() + 1), checkNum(date.getDate())].join('-') + '  ' + [checkNum(date.getHours()), checkNum(date.getMinutes()), checkNum(date.getSeconds())].join(':');
    return tt;
}

function capitalizes(value) {
    var date = new Date(parseInt(value));
    var tt = [date.getFullYear(), checkNum(date.getMonth() + 1), checkNum(date.getDate())].join('-');
    return tt;
}

function capitalizeo(value) {
    var date = new Date(parseInt(value));
    var tt = [checkNum(date.getHours()), checkNum(date.getMinutes())].join(':');
    return tt;
}

function capitalizm(value) {
    var date = new Date(parseInt(value));
    var tt = [checkNum(date.getMonth() + 1)].join(':');
    if (tt < 9) {
        tt = parseInt(tt);
    }
    return tt;
}

function capitalizec(value) {
    var date = new Date(parseInt(value));
    var tt = date.getFullYear()+"年"+checkNum(date.getMonth() + 1)+"月"+checkNum(date.getDate())+"日";
    return tt;
}
//判断位数个位前面补0
function checkNum(val) {
    if (val < 10) {
        return '0' + val;
    } else {
        return val;
    }
}
//判断是否微信浏览器打开
function is_weixin() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}
/**
 * 清理用户数据
 */
function clearUserData() {
    sessionStorage.removeItem("user_data");
}

//滚动到顶部
$.fn.scrollTo = function(options) {
    var defaults = {
        toT: 0, //滚动目标位置
        durTime: 500, //过渡动画时间
        delay: 30, //定时器时间
        callback: null //回调函数
    };
    var opts = $.extend(defaults, options),
        timer = null,
        _this = this,
        curTop = _this.scrollTop(), //滚动条当前的位置
        subTop = opts.toT - curTop, //滚动条目标位置和当前位置的差值
        index = 0,
        dur = Math.round(opts.durTime / opts.delay),
        smoothScroll = function(t) {
            index++;
            var per = Math.round(subTop / dur);
            if (index >= dur) {
                _this.scrollTop(t);
                window.clearInterval(timer);
                if (opts.callback && typeof opts.callback == 'function') {
                    opts.callback();
                }
                return;
            } else {
                _this.scrollTop(curTop + index * per);
            }
        };
    timer = window.setInterval(function() {
        smoothScroll(opts.toT);
    }, opts.delay);
    return _this;
};

// 金额大写
function convertCurrency(money) {
    //汉字的数字
    var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
    //基本单位
    var cnIntRadice = new Array('', '拾', '佰', '仟');
    //对应整数部分扩展单位
    var cnIntUnits = new Array('', '万', '亿', '兆');
    //对应小数部分单位
    var cnDecUnits = new Array('角', '分', '毫', '厘');
    //整数金额时后面跟的字符
    var cnInteger = '整';
    //整型完以后的单位
    var cnIntLast = '元';
    //最大处理的数字
    var maxNum = 999999999999999.9999;
    //金额整数部分
    var integerNum;
    //金额小数部分
    var decimalNum;
    //输出的中文金额字符串
    var chineseStr = '';
    //分离金额后用的数组，预定义
    var parts;
    if (money == '') {
        return '';
    }
    money = parseFloat(money);
    if (money >= maxNum) {
        //超出最大处理数字
        return '';
    }
    if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
    }
    //转换为字符串
    money = money.toString();
    if (money.indexOf('.') == -1) {
        integerNum = money;
        decimalNum = '';
    } else {
        parts = money.split('.');
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
    }
    //获取整型部分转换
    if (parseInt(integerNum, 10) > 0) {
        var zeroCount = 0;
        var IntLen = integerNum.length;
        for (var i = 0; i < IntLen; i++) {
            var n = integerNum.substr(i, 1);
            var p = IntLen - i - 1;
            var q = p / 4;
            var m = p % 4;
            if (n == '0') {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    chineseStr += cnNums[0];
                }
                //归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
            }
            if (m == 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
            }
        }
        chineseStr += cnIntLast;
    }
    //小数部分
    if (decimalNum != '') {
        var decLen = decimalNum.length;
        for (var i = 0; i < decLen; i++) {
            var n = decimalNum.substr(i, 1);
            if (n != '0') {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
            }
        }
    }
    if (chineseStr == '') {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (decimalNum == '') {
        chineseStr += cnInteger;
    }
    return chineseStr;
}
/**  
 * 将以base64的图片url数据转换为Blob  
 * @param urlData  
 *            用url方式表示的base64图片数据  
 */
function convertBase64UrlToBlob(urlData) {

    var bytes = window.atob(urlData.split(',')[1]); //去掉url的头，并转换为byte  

    //处理异常,将ascii码小于0的转换为大于0  
    var ab = new ArrayBuffer(bytes.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < bytes.length; i++) {
        ia[i] = bytes.charCodeAt(i);
    }

    return new Blob([ab], { type: 'image/png' });
}

// 公用报错
function checkcode(code) {
    if (code == 303 || code == 500) {
        $.toast("出错啦！请您稍后重试");
        return true;
    } else {
        return false;
    }
}
// 金额三位分割
function format_number(s, n) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
    var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
    t = "";
    for (i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("") + "." + r;
}
//银行卡四位分隔
function bancard(val) {
    var cardnum = val.replace(/[\s]/g, '').replace(/(\d{4})(?=\d)/g, "$1 ");
    return cardnum;
}

// 获取是否存在公共小图
function isTargetOne(arr) {
    var isTargetOne = false;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].referTarget == 1) {
            isTargetOne = true;
        }
    }
    return isTargetOne;
}
